<template>
    <div class="sys-dictType-container">
        <el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
            <template #header>
                <div style="color: #fff">
                    <el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
                    </el-icon>
                    <span> {{ props.title }} </span>
                </div>
            </template>
            <el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="物料编码" prop="materialCode"
                            :rules="[{ required: true, message: '编码不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.materialCode" placeholder="物料编码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="物料名称" prop="materialName"
                            :rules="[{ required: true, message: '物料名称不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.materialName" placeholder="物料名称" clearable />
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="批次" prop="batch"
                            :rules="[{ required: true, message: '批次不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.batch" placeholder="批次" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="参考号" prop="reference"
                            :rules="[{ required: true, message: '参考号不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.reference" placeholder="参考号" clearable />
                        </el-form-item>
                    </el-col>
                     <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="数量" prop="number"
                            :rules="[{ required: true, message: '数量不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.number" placeholder="数量" clearable />
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="收货日期" prop="dateofReceipt"
                            :rules="[{ required: true, message: '收货日期不能为空', trigger: 'blur' }]">
                            <el-date-picker v-model="state.ruleForm.dateofReceipt" type="datetime" placeholder="请选择收货日期"
                                clearable /> 
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="箱条码" prop="boxCode"
                            :rules="[{ required: true, message: '箱条码不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.boxCode" placeholder="箱条码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="批次" prop="batch"
                            :rules="[{ required: true, message: '批次不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.batch" placeholder="批次" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="母托盘" prop="palcode"
                            :rules="[{ required: true, message: '母托盘码下拉联动不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.palcode" placeholder="母托盘" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="子托盘" prop="subtray"
                            :rules="[{ required: true, message: '子托盘盘码下拉联动不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.subtray" placeholder="母托盘" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="当前库位编码" prop="warehousePositionCode"
                            :rules="[{ required: true, message: '当前库位编码不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.warehousePositionCode" placeholder="当前库位编码" clearable />
                        </el-form-item>
                    </el-col> 

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="库存状态" prop="isEnable"
                            :rules="[{ required: false, message: '必选', trigger: 'blur' }]">
                            <!-- v-if="state.ruleForm.name != null" 加在是否的item上 -->
                            <el-radio-group v-model="state.ruleForm.status">
                                <el-radio :value="0">冻结</el-radio>
                                <el-radio :value="1">正常</el-radio>
                                <el-radio :value="2">暂存入库</el-radio>
                                <el-radio :value="3">出库完成</el-radio>

                            </el-radio-group>
                        </el-form-item>
                    </el-col>
              
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="EditPosition">


import { AccountTypeEnum, UpdateDictTypeInput } from '/@/api-services/models';
import { useUserInfo } from '/@/stores/userInfo';
import { InventoryManagementApi } from '/@/api-services/apis/warehouse-inventory-api'; // 导入 API 服务
import { InventoryManagement  } from '/@/api-services/models/warehouse-inventory-input'; // 导入获取实体

import { ref, onMounted,reactive } from 'vue';
import { getAPI } from '/@/utils/axios-utils';
import { ElMessageBox, ElMessage,dayjs } from 'element-plus';


import { WarehouseInput } from '/@/api-services/models/warehouse-input'; // 导入仓库实体添加编辑下拉
import { WarehouseApi } from '/@/api-services/apis/warehouse-api';



const ruleFormRef = ref();
const state = reactive({
	loading: false,
    isShowDialog: false,
    ruleForm: {} as InventoryManagement,
 warehouseList :Array<WarehouseInput>(),

});


// 在 onMounted 生命周期钩子中获取仓库数据
onMounted(async () => {
    try {
        state.loading = true;
        // 调用 API 获取仓库数据
        const response = await getAPI(WarehouseApi).apiWarehousePagePost({ page: 1,
pageSize: 1000, isEnable: 1 });
            state.warehouseList = response.data.result?.items||[];
            console.log(state.warehouseList);
    } catch (error) {
        console.error('获取仓库数据失败', error);
    }
});

// 打开弹窗
const openDialog = (row: any) => {
    state.ruleForm = JSON.parse(JSON.stringify(row));
    console.log(JSON.stringify(row));

    state.isShowDialog = true;
    ruleFormRef.value?.resetFields();
};

const props = defineProps({
    title: String,
});
const emits = defineEmits(['handleQuery']);


// 关闭弹窗
const closeDialog = () => {
    emits('handleQuery');
    state.isShowDialog = false;
};

// 取消
const cancel = () => {
    state.isShowDialog = false;
};

// 提交
const submit = () => {
    ruleFormRef.value.validate(async (valid: boolean) => {
        if (!valid) return;
   
				console.log(state.ruleForm);
        if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
            alert(useUserInfo.name);
            await getAPI(InventoryManagementApi).apiInventoryManagementUpdatePost(state.ruleForm);
            ElMessage({
                message: '修改成功',
                type: 'success',
            })
        } else {
            await getAPI(InventoryManagementApi).apiInventoryManagementAddPost(state.ruleForm);
            ElMessage({
                message: '添加成功',
                type:'success',
            })
        }
        closeDialog();
    });
};

// 导出对象
defineExpose({ openDialog });
</script>
